import { defineStore } from 'pinia'
import { store } from '../index'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'

const { wsCache } = useCache()

export interface leftMenuState {
  leftMenu: AppRouteRecordRaw[],
  topMenuActiveIndex: Number
}

export const useLeftMenuStore = defineStore('leftMenu', {
  state: (): leftMenuState => ({
    leftMenu: [],
    topMenuActiveIndex: 0
  }),
  getters: {
    getLeftMenu(): AppRouteRecordRaw[] {
      return this.leftMenu
    },
    getTopMenuActiveIndex(): Number {
      return this.topMenuActiveIndex
    }
  },
  actions: {
    setCacheLeftMenu(): void {
      const menuItem = wsCache.get(CACHE_KEY.LEFT_MENU)
      this.leftMenu = menuItem
    },
    setLeftMenu(menuItem: AppRouteRecordRaw[]): void {
      this.leftMenu = menuItem
      wsCache.set(CACHE_KEY.LEFT_MENU, menuItem)
    },
    setTopMenuActiveIndex(index: Number): void {
      this.topMenuActiveIndex = index
    }
  }
})

export const useLeftMenuStoreWithOut = () => {
  return useLeftMenuStore(store)
}
